/*
 * PhoneBook.java
 *
 * Created on May 15, 2008, 2:12 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package morseme;

/**
 *
 * @author tt
 */
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.util.*;

public class PhoneBook extends List implements CommandListener{

private MainMenu menu;
private static final Command addCmd = new Command("Add Number", Command.OK, 1);
private static final Command clearCmd = new Command("Clear Book", Command.OK, 1);
private static final Command delCmd = new Command("Delete Number", Command.OK, 1);
private static final Command backCmd = new Command("Back", Command.BACK, 1);

private static RecordStore rs;
private Vector contactList = new Vector(1);

public PhoneBook(MainMenu menu) {

super("Phone Book", List.IMPLICIT);
this.menu = menu;

this.addCommand(addCmd);
this.addCommand(delCmd);
this.addCommand(clearCmd);
this.addCommand(backCmd);

this.setCommandListener(this);
displayContacts();
}

private void displayContacts(){

contactList.removeAllElements();
String[] contact = this.loadBook();
for(int i = 0; i < contact.length; i++){
int idx = contact[i].indexOf(";");
if(idx == -1){
continue;
}

String name = contact[i].substring(0, idx);
String rest = contact[i].substring(idx+1, contact[i].length());
idx = rest.indexOf(";");
String number = rest.substring(0, idx);
String recordID = rest.substring(idx+1, rest.length());

this.append(name + "\nNumber: " + number + "\nIndex:" + recordID , null);
contactList.addElement(recordID);
}
}

public void updatePhoneBook(){

this.deleteAll();
displayContacts();
}

public void commandAction(Command c, Displayable d) {

if(c == addCmd){

Display.getDisplay(MorseME.getMIDlet()).setCurrent(new AddNumberForm(this));

}else if(c == backCmd){

Display.getDisplay(MorseME.getMIDlet()).setCurrent(this.menu);

}else if(c == clearCmd){

clearBook();
this.deleteAll();

}else if(c == delCmd){

int selected_index = this.getSelectedIndex();
if(selected_index != -1){

deleteNumber(selected_index);
this.delete(selected_index);
}

}
}

private void deleteNumber(int list_index){
try{
System.out.println("DELETE LIST INDEX:" + list_index);
String recordIDStr = contactList.elementAt(list_index).toString();
rs = RecordStore.openRecordStore("morsemebook8", true);
System.out.println("RECORD ID: " + recordIDStr);
int recordID = Integer.parseInt(recordIDStr);
rs.deleteRecord(recordID);

rs.closeRecordStore();

}catch(Exception ex){

ex.printStackTrace();
}
}

public void clearBook(){

try{
rs = RecordStore.openRecordStore("morsemebook8", true);
for(int i = 0; i < rs.getNumRecords(); i++){
rs.deleteRecord(i+1);
}
rs.closeRecordStore();
rs.deleteRecordStore("morsemebook8");

}catch(Exception ex){

ex.printStackTrace();
}
}

public String[] loadBook() {
Vector tmp = new Vector(1);
String[] book = null;
try {
rs = RecordStore.openRecordStore("morsemebook8", true);

int cnum = rs.getNumRecords();
RecordEnumeration recEnum = rs.enumerateRecords(null, null, false);
System.out.println("LOAD SIZE=" + recEnum.numRecords());
while(recEnum.hasNextElement()){
tmp.addElement(recEnum.nextRecord());
}
// for(int i = 0; i < cnum; i++){
// try{
// byte[] b = rs.getRecord(i + 1);
// tmp.addElement(b);
// }catch(InvalidRecordIDException ex){
// continue;
// }
// }
book = new String[tmp.size()];
for(int i = 0; i < book.length; i++){

book[i] = new String((byte[])tmp.elementAt(i));
}
}
catch (Exception ex) {
ex.printStackTrace();
}finally{
try {
rs.closeRecordStore();
}
catch (Exception ex) {
ex.printStackTrace();
}
}

return book;
}

public void saveNumber(String name, String number) {
try {
rs = RecordStore.openRecordStore("morsemebook8", true);
String tmp = "@";
int idx = rs.addRecord(tmp.getBytes(), 0, tmp.length());
String contactStr = name + ";" + number + ";" + idx;
byte[] bContact = contactStr.getBytes();
rs.setRecord(idx, bContact, 0, bContact.length);
}
catch (Exception ex) {

ex.printStackTrace();
}finally{
try {
rs.closeRecordStore();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
}

